//
//  CollectionViewController.m
//  Unicorn
//
//  Created by xalo on 16/5/3.
//  Copyright © 2016年 bhj_jun. All rights reserved.
//

#import "CollectionViewController.h"

typedef enum {
    
    allDataType,
    likeDataType
}collectType;

@interface CollectionViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,assign)collectType collType;

@property (nonatomic,retain)UIScrollView *scrollView;

@property (nonatomic,retain)UISegmentedControl *segmentControl;//分段控制器

@property (nonatomic,retain)UITableView *allCollection;//所有收藏

@property (nonatomic,retain)UITableView *likeCollection;//喜欢收藏

@property (nonatomic,retain)NSMutableArray *allDataArr;//所有 收藏数据

@property (nonatomic,retain)NSMutableArray *likeDataArr;//喜欢 收藏数据

@end


@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self viewSetting];
    
    //    self.collType = allDataType;
    
    [self.allCollection registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.likeCollection registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - - 懒加载

-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds];
        _scrollView.contentSize = CGSizeMake(kWidth * 2, 0);
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

-(UITableView *)allCollection
{
    if (!_allCollection) {
        _allCollection = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _allCollection.delegate = self;
        _allCollection.dataSource = self;
    }
    return _allCollection;
}

-(UITableView *)likeCollection
{
    if (!_likeCollection) {
        _likeCollection = [[UITableView alloc] initWithFrame:CGRectMake(kWidth, 0, kWidth, kHeight) style:UITableViewStylePlain];
        _likeCollection.delegate = self;
        _likeCollection.dataSource = self;
    }
    return _likeCollection;
}

-(NSMutableArray *)allDataArr
{
    if (!_allDataArr) {
        _allDataArr=[NSMutableArray array];
    }
    return _allDataArr;
}

-(NSMutableArray *)likeDataArr
{
    if (!_likeDataArr) {
        _likeDataArr=[NSMutableArray array];
    }
    return _likeDataArr;
}

//界面设置
-(void)viewSetting
{
    //分段控制器
    NSArray * segmentArr = [NSArray arrayWithObjects:@"所有收藏",@"喜欢",nil];
    self.segmentControl = [[UISegmentedControl alloc]initWithItems:segmentArr];
    self.segmentControl.frame = CGRectMake(kWidth/2-150, 0, 150, 30);
    self.segmentControl.center =self.navigationController.navigationBar.center;
    [self.segmentControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.navigationController.navigationBar addSubview:self.segmentControl];
    
    //返回
    self.navigationItem.leftBarButtonItem = ({
        
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(backItemAction:)];
        item;
    });
    
    [self.view addSubview:self.scrollView];
    
    
    
}

#pragma mark - - 事件回调
//返回事件
-(void)backItemAction:(UIBarButtonItem *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//分段控制器事件
-(void)segmentAction:(UISegmentedControl *)sender
{
    switch (sender.selectedSegmentIndex) {
        case 0:{
            [self.scrollView setContentOffset:CGPointMake(0, 0)];
            break;
        }
        case 1:{
            [self.scrollView setContentOffset:CGPointMake(kWidth, 0)];
            break;
        }
        default:
            break;
    }
}

#pragma mark - - tableView代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.allCollection) {
        
        //        return self.allDataArr.count;
        return 20;
    }else if (tableView == self.likeCollection){
        
        //        return self.likeDataArr.count;
        return 20;
    }
    return 20;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    if (tableView == self.allCollection) {
        
        //cell.textLabel.text = self.allDataArr[indexPath.row];
        self.collType = allDataType;
        cell.textLabel.text = [NSString stringWithFormat:@"TEST单元格(allData)－－%ld",indexPath.row];
        
    }else if (tableView == self.likeCollection){
        
        //cell.textLabel.text = self.likeDataArr[indexPath.row];
        self.collType = likeDataType;
        cell.textLabel.text = [NSString stringWithFormat:@"TEST单元格(likeData)－－%ld",indexPath.row];
        
    }
    
    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.scrollView) {
        
        if (scrollView.contentOffset.x == 0) {
            
            self.segmentControl.selectedSegmentIndex = 0;
            [self.scrollView addSubview:self.allCollection];
            self.collType = allDataType;
        }else if (scrollView.contentOffset.x == kWidth){
            
            self.segmentControl.selectedSegmentIndex = 1;
            [self.scrollView addSubview:self.likeCollection];
            self.collType = likeDataType;
        }
    }
}


@end
